package com.gscitysfy.cus.drain.modules.inspection.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.security.utils.SecurityUtils;
import com.gscitysfy.cus.drain.annotations.DictTranslation;
import com.gscitysfy.cus.drain.annotations.FieldParam;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.common.vo.PageVo;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionMnOrder;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionMnOrderService;
import com.gscitysfy.cus.drain.modules.inspection.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.gscitysfy.cus.drain.modules.inspection.service.impl.DrainInspectionMnOrderServiceImpl.PLATFORM_ADMIN;

/**
 * <p>
 * 维修养护管理 -> 维养任务管理
 * </p>
 *
 * @author Administrator
 * @since 2022-10-14 16:15:41
 */

@RestController
@RequestMapping("/inspection/order")
@Tag(name = "维修养护管理 -> 维养任务管理")
public class DrainInspectionMnOrderController {

    @Resource
    private IDrainInspectionMnOrderService orderService;

    /**
     * 分页获取维养任务单列表
     *
     * @param queryVo 条件
     * @return Result 分页数据
     */
    @Operation(summary = "获取维养任务单列表", description = "获取维养任务单列表接口")
    @PostMapping("/list")
    @DictTranslation({
            @FieldParam(dictType = DicConstant.PLAN_OBJECT_TYPE, targetField = "cmProblemType")
    })
    public Result<IPage<DrainInspectionMnOrder>> pageList(@RequestBody DrainInspectionMnOrderQueryVo queryVo) {
        String name = SecurityUtils.getPrincipal().getName();
        String userId = SecurityUtils.getPrincipal().getUserId();
        if (StringUtils.isNotEmpty(name) && !PLATFORM_ADMIN.equals(name)) {
            queryVo.setUserId(userId);
        }
        return Result.content(orderService. pageList(queryVo));
    }

    /**
     * 获取维养任务单详情
     *
     * @param id 任务单id
     * @return Result 结果数据
     */
    @Operation(summary = "获取维养任务单详情", description = "获取维养任务单详情接口")
    @GetMapping("/detail")
    public Result<DrainInspectionMnOrderDetailVo> getDetailById(Long id) {
        return Result.content(orderService.detailById(id));
    }

    /**
     * 删除维养任务单
     * @param ids 任务单id 多个id逗号分割
     * @return Result 结果数据
     */
    @Operation(summary = "删除维养任务单", description = "删除维养任务单接口")
    @PostMapping("/delete")
    public Result<Boolean> deleteByIds(String ids) {
        return orderService.deleteByIds(ids) ? Result.success() : Result.failure();
    }

    /**
     * 复核任务单
     *
     * @param id      计划id
     * @param state   审核状态
     * @param comment 审核意见
     * @return Result 结果数据
     */
    @Operation(summary = "复核维修任务单", description = "复核维修任务单接口")
    @PostMapping("/reAudit")
    public Result<Boolean> reAudit(Long id, Integer state, String comment) {
        return orderService.reAudit(id, state, comment) ? Result.success() : Result.failure();
    }

    /**
     * 获取今日待处置的任务单
     *
     * @param page 分页信息
     * @return Result 结果数据
     */
    @Operation(summary = "获取今日待处置的任务单", description = "获取今日待处置的任务单接口")
    @PostMapping("/todayTodo")
    @DictTranslation({
            @FieldParam(dictType = DicConstant.PLAN_OBJECT_TYPE, targetField = "cmProblemType")
    })
    public Result<IPage<DrainInspectionMnOrder>> todayTodo(PageVo page) {
        return Result.content(orderService.todayTodo(page));
    }

    /**
     * 勾选导出
     * @param ids      任务单id，多个id逗号分隔
     * @param response 响应
     */
    @Operation(summary = "导出维修任务Excel", description = "导出维修任务")
    @PostMapping("/export")
    public void exportInfos(String ids, HttpServletResponse response) {
        orderService.exportInfos(ids, response);
    }

    /**
     * 重新生成维养单
     *
     * @param id 维养单id
     * @return Result 结果数据
     */
    @Operation(summary = "重新生成维养单的详情", description = "重新生成维养单的详情接口")
    @PostMapping("/reGenerateDetail")
    public Result<DrainInspectionMnOrderVo> reGenerateDetail(Long id) {
        return Result.content(orderService.reGenerateDetail(id));
    }

    /**
     * 重新生成维养单
     * @param orderVo 维养单信息
     * @return Result
     */
    @Operation(summary = "重新生成维养单", description = "重新生成维养单接口")
    @PostMapping("/reGenerate")
    public Result<Boolean> reGenerate(@RequestBody DrainInspectionMnOrderVo orderVo) {
        return orderService.reGenerate(orderVo) ? Result.success() : Result.failure();
    }

    /**
     * 小程序处置接口
     *
     * @param disposeVo 处置信息
     * @return Result 结果数据
     */
    @Operation(summary = "维养单开始处置", description = "维养单开始处置接口")
    @PostMapping("/dispose")
    public Result<Boolean> dispose(@RequestBody DrainInspectionMnOrderDisposeVo disposeVo) {
        return orderService.dispose(disposeVo) ? Result.success() : Result.failure();
    }

    /**
     * app端问题依据统计
     *
     * @return Result 结果数据
     */
    @PostMapping("/countProblemBasis")
    @Operation(summary = "app端问题依据统计", description = "app端问题依据统计接口")
    public Result<List<MiniProgramCountVo>> countProblemBasis() {
        return Result.content(orderService.countProblemBasis());
    }

    /**
     * app端获取维养任务单列表
     *
     * @param queryVo 查询条件
     * @return Result 结果数据
     */
    @Operation(summary = "app端获取维养任务单列表", description = "app端获取维养任务单列表接口")
    @PostMapping("/appList")
    @DictTranslation({
            @FieldParam(dictType = DicConstant.PLAN_OBJECT_TYPE, targetField = "cmProblemType")
    })
    public Result<IPage<DrainInspectionMnOrder>> getAppPageList(@RequestBody AppQueryVo queryVo) {
        String name = SecurityUtils.getPrincipal().getName();
        String userId = SecurityUtils.getPrincipal().getUserId();
        if (StringUtils.isNotEmpty(name) && !PLATFORM_ADMIN.equals(name)) {
            queryVo.setUserId(userId);
        }
        return Result.content(orderService.getAppPageList(queryVo));
    }

    @Operation(summary = "更新维养单", description = "更新维养单接口")
    @PostMapping("/changeState")
    public Result<Boolean> update(Long id) {
        return orderService.updateState(id) ? Result.success() : Result.failure();
    }

    @PostMapping("/miniProgramCount")
    @Operation(summary = "小程序统计", description = "小程序统计接口")
    public Result<List<MiniProgramCountVo>> miniProgramCount() {
        return Result.content(orderService.miniProgramCount());
    }
}
